package com.ruoyi.basedocument.service.impl;

import java.util.List;

import com.ruoyi.basedocument.domain.BomRawmaterial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basedocument.domain.BomMaterialhalfentry;
import com.ruoyi.basedocument.mapper.BomMaterialhalfMapper;
import com.ruoyi.basedocument.domain.BomMaterialhalf;
import com.ruoyi.basedocument.service.IBomMaterialhalfService;

/**
 * BOM物料半成品Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-05
 */
@Service
public class BomMaterialhalfServiceImpl implements IBomMaterialhalfService 
{
    @Autowired
    private BomMaterialhalfMapper bomMaterialhalfMapper;

    /**
     * 查询BOM物料半成品
     * 
     * @param fid BOM物料半成品主键
     * @return BOM物料半成品
     */
    @Override
    public BomMaterialhalf selectBomMaterialhalfByFid(String fid)
    {
        return bomMaterialhalfMapper.selectBomMaterialhalfByFid(fid);
    }

    /**
     * 查询BOM物料半成品列表
     * 
     * @param bomMaterialhalf BOM物料半成品
     * @return BOM物料半成品
     */
    @Override
    public List<BomMaterialhalf> selectBomMaterialhalfList(BomMaterialhalf bomMaterialhalf)
    {
        return bomMaterialhalfMapper.selectBomMaterialhalfList(bomMaterialhalf);
    }

    /**
     * 查询BOM物料半成品列表
     *
     * @param bomMaterialhalf BOM物料半成品
     * @return BOM物料半成品
     */
    @Override
    public List<BomMaterialhalf> selectBomMaterialhalfList2(BomMaterialhalf bomMaterialhalf)
    {
        return bomMaterialhalfMapper.selectBomMaterialhalfList2(bomMaterialhalf);
    }

    /**
     * 新增BOM物料半成品
     * 
     * @param bomMaterialhalf BOM物料半成品
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBomMaterialhalf(BomMaterialhalf bomMaterialhalf)
    {
        BomMaterialhalf obj = new BomMaterialhalf();
        obj.setCfmaterialnumber(bomMaterialhalf.getCfmaterialnumber());
        List<BomMaterialhalf> bomMaterialhalves = bomMaterialhalfMapper.selectBomMaterialhalfList2(obj);
        if(bomMaterialhalves!=null&&bomMaterialhalves.size()!=0){
            throw new RuntimeException("编码已存在:"+bomMaterialhalf.getCfmaterialnumber());
        }
        int rows = bomMaterialhalfMapper.insertBomMaterialhalf(bomMaterialhalf);
        insertBomMaterialhalfentry(bomMaterialhalf);
        return rows;
    }

    /**
     * 修改BOM物料半成品
     * 
     * @param bomMaterialhalf BOM物料半成品
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBomMaterialhalf(BomMaterialhalf bomMaterialhalf)
    {
        bomMaterialhalfMapper.deleteBomMaterialhalfentryByFParentID(bomMaterialhalf.getFid());
        insertBomMaterialhalfentry(bomMaterialhalf);
        return bomMaterialhalfMapper.updateBomMaterialhalf(bomMaterialhalf);
    }

    /**
     * 批量删除BOM物料半成品
     * 
     * @param fids 需要删除的BOM物料半成品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBomMaterialhalfByFids(String[] fids)
    {
        int i = bomMaterialhalfMapper.deleteBomMaterialhalfByFids(fids);
        if(fids.length!=i){
            for (int i1 = 0; i1 < fids.length; i1++) {
                BomMaterialhalf bomMaterialhalf = bomMaterialhalfMapper.selectBomMaterialhalfByFid(fids[i1]);
                if(bomMaterialhalf==null){
                    bomMaterialhalfMapper.deleteBomMaterialhalfentryByFParentID(fids[i1]);
                }
            }
        }else{
            int i1 = bomMaterialhalfMapper.deleteBomMaterialhalfentryByFParentIDs(fids);
        }
        return i;
    }

    /**
     * 删除BOM物料半成品信息
     * 
     * @param fid BOM物料半成品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBomMaterialhalfByFid(String fid)
    {
        bomMaterialhalfMapper.deleteBomMaterialhalfentryByFParentID(fid);
        return bomMaterialhalfMapper.deleteBomMaterialhalfByFid(fid);
    }

    /**
     * 新增物料BOM原材料分录信息
     * 
     * @param bomMaterialhalf BOM物料半成品对象
     */
    public void insertBomMaterialhalfentry(BomMaterialhalf bomMaterialhalf)
    {
        List<BomMaterialhalfentry> bomMaterialhalfentryList = bomMaterialhalf.getBomMaterialhalfentryList();
        String fid = bomMaterialhalf.getFid();
        if (StringUtils.isNotNull(bomMaterialhalfentryList))
        {
            List<BomMaterialhalfentry> list = new ArrayList<BomMaterialhalfentry>();
            for (BomMaterialhalfentry bomMaterialhalfentry : bomMaterialhalfentryList)
            {
                bomMaterialhalfentry.setFparentid(fid);
                list.add(bomMaterialhalfentry);
            }
            if (list.size() > 0)
            {
                bomMaterialhalfMapper.batchBomMaterialhalfentry(list);
            }
        }
    }
}
